//重写数组
//1.获取原来的数组方法
let oldArrayProtoMethods = Array.prototype;

//2.继承
export let ArrayMethods = Object.create(oldArrayProtoMethods);

const methods = [
  "push",
  "pop",
  "unshift",
  "shift",
  "splice",
  "sort",
  "reverse",
];

methods.forEach((method) => {
  ArrayMethods[method] = function (...args) {
    // console.log("数组劫持");
    const result = oldArrayProtoMethods[method].apply(this, args);
    // 追加数据
    let inserted;
    switch (method) {
      case "push":
      case "unshift":
        inserted = args;
        break;
      case "splice":
        inserted = args.slice(2);
        break;
      default:
        break;
    }
    if(inserted){
        this.__ob__.observeArray(inserted)
    }
    return result;
  };
});
